/**
 * @图片上传
 */
const fs = require('fs')
const {
  mysql
} = require('../qcloud')
const exif = require('exif')
module.exports = async (ctx) => {
  // ctx.body = "你好"
  let timestap = Date.now()
  var path = '../static/images/temp/' + timestap + '.png'
  //console.log(ctx.request.body, path);
  var base64 = ctx.request.body.base64.replace(/^data:image\/\w+;base64,/, ""); //去掉图片base64码前面部分data:image/png;base64
  var dataBuffer = new Buffer(base64, 'base64'); //把base64码转成buffer对象，
  // console.log('dataBuffer是否是Buffer对象：' + Buffer.isBuffer(dataBuffer));
  //   console.log('dataBuffer是否是Buffer对象：' + Buffer.from(dataBuffer));
  console.log(path);
  fs.writeFile(path, dataBuffer, function (err) { //用fs写入文件
    if (err) {
      console.log(err);
    } else {
      //console.log('写入成功！');
    }
  })
  //   console.log(path);
  let exifInfo = await getExif(path).then(res => res)
  console.log(exifInfo);
  ctx.state.data = {
    msg: 'success',
    url: `https://wx.2xur7.cn/static/images/temp/${timestap}.png`,
    exifInfo
  }
}


async function getExif(path) {
  return new Promise((resolve, reject) => {
    new exif({
      image: path
    }, function (err, data) {
      if (err) {
        reject(err)
      } else {
        resolve(data)
      }
    })
  })
}
